package cn.itsource.org.service.impl;

import cn.itsource.basic.service.impl.BaseServiceImpl;
import cn.itsource.basic.util.PageList;
import cn.itsource.basic.util.TreeUtil;
import cn.itsource.org.domain.Department;
import cn.itsource.org.mapper.DepartmentMapper;
import cn.itsource.org.query.DepartmentQuery;
import cn.itsource.org.service.IDepartmentService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
//Propagation.SUPPORTS:支持别人事务
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
public class DepartmentServiceImpl extends BaseServiceImpl<Department,DepartmentQuery> implements IDepartmentService {


    /**
     * 注入mapper
     * @return
     */
    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @Transactional
    public void save(Department department) {
        //insertSelective 只有被设置了值的字段才会被sql语句操作
        departmentMapper.insertSelective(department);
        //修改  要维护parentId  要为dir_path
        this.update(department);
    }

    @Override
    @Transactional
    public void update(Department department) { //4
        //效果：1.要维护parentId   2.要为dir_path  /3/8/10/4
        //传递的值  parentIds :  [3,8,10]    [3]    []

        //获取集合的最后以为，作为parentId的数据
        Long[] parentIds = department.getParentIds();

        //性能比String高  Common part can be extracted removing branch
        StringBuilder sb = new StringBuilder("");

        // /4
        if (parentIds != null && parentIds.length > 0){
            //获取最后一位  3  索引为2  3-1
            Long parentId = parentIds[parentIds.length - 1];
            department.setParentId(parentId);

            //dir_path
            for (Long id : parentIds) {
                sb.append("/").append(id); //   /3/8/10
            }

        }
        sb.append("/").append(department.getId()); //   /3/8/10/4

        //设置拼接好的dirPath
        department.setDirPath(sb.toString());

        //只有被设置了值的字段才会被sql语句操作
        departmentMapper.updateByPrimaryKeySelective(department);
    }



    /**
     * 使用分页插件做分页
     * @param departmentQuery
     * @return
     */
    @Override
    public PageList<Department> findPage(DepartmentQuery departmentQuery) {


        //1.使用分页插件   1,2步中间不能写代码
        PageHelper.startPage(departmentQuery.getCurrentPage(),departmentQuery.getPageSize());
        //2.执行查询语句
        List<Department> departments = departmentMapper.findPage(departmentQuery);

        //3.封装成一个pageInfo对象（分页插件提供的）
        PageInfo<Department> pageInfo = new PageInfo<>(departments);

        //4.返回我们自己定义的PageList对象  total list
        PageList<Department> pageList = new PageList<>(pageInfo.getTotal(), pageInfo.getList());

        //返回结果
        return pageList;
    }

    @Override
    public List<Department> getTree() {
        //1.获取到所有的部门数据
        List<Department> departments = departmentMapper.selectAll();

        //2.通过递归工具类把数据转换成无限级结构
        List<Department> departmentList = TreeUtil.getTree(departments);
        //返回结果
        return departmentList;
    }


}
